package com.gdut.wifidemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.gdut.wifidemo.contract.WifiContract;
import com.gdut.wifidemo.data.entity.Result;
import com.gdut.wifidemo.presenter.WifiPresenter;
import com.gdut.wifidemo.utils.LogUtils;

public class MainActivity extends AppCompatActivity implements WifiContract.View {

    private Button mUpdateButton;
    private TextView mPositionTextView;
    private TextView mLogTextView;
    private ScrollView mLogScrollView;
    private WifiContract.Presenter mWifiPresenter;
    private WifiManager mWifiManager;
    private BroadcastReceiver mWifiReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent == null || intent.getAction() == null) {
                return;
            }
            if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
                LogUtils.d("WIFI_STATE_CHANGED_ACTION");
                int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, -1);
                switch (state) {
                    case WifiManager.WIFI_STATE_DISABLING:
                        LogUtils.d("WIFI_STATE_DISABLING");
                        break;
                    case WifiManager.WIFI_STATE_DISABLED:
                        LogUtils.d("WIFI_STATE_DISABLED");
                        break;
                    case WifiManager.WIFI_STATE_ENABLING:
                        LogUtils.d("WIFI_STATE_ENABLING");
                        break;
                    case WifiManager.WIFI_STATE_ENABLED:
                        LogUtils.d("WIFI_STATE_ENABLED");
                        break;
                    case WifiManager.WIFI_STATE_UNKNOWN:
                        LogUtils.d("WIFI_STATE_UNKNOWN");
                        break;
                    default:
                        LogUtils.d("WIFI_STATE_DEFAULT");
                        break;
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initParam();
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        registerReceiver(mWifiReceiver, filter);
        enableWifi();
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(mWifiReceiver);
        disableWifi();
    }

    private void initView() {
        mUpdateButton = (Button) findViewById(R.id.id_update_button);
        mPositionTextView = (TextView) findViewById(R.id.id_position_text);
        mLogTextView = (TextView) findViewById(R.id.id_log_text);
        mLogScrollView = (ScrollView) findViewById(R.id.id_log_scroll_view);
        mUpdateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.d("start...");
                mWifiPresenter.start();
            }
        });
    }

    private void initParam() {
        new WifiPresenter(this);
    }

    @Override
    public void setPresenter(@NonNull WifiContract.Presenter presenter) {
        this.mWifiPresenter = presenter;
    }

    @Override
    public void showMessage(String message) {
        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void updateButtonStatus(String text, boolean enable) {
        mUpdateButton.setText(text);
        mUpdateButton.setEnabled(enable);
    }

    @Override
    public void updatePositionText(String position) {
        mPositionTextView.setText(position);
    }

    @Override
    public void log(String log) {
        mLogTextView.append(log);
        mLogTextView.append("\r\n");
        mLogScrollView.post(new Runnable() {
            @Override
            public void run() {
                mLogScrollView.fullScroll(ScrollView.FOCUS_DOWN);
            }
        });
    }

    @Override
    public void enableWifi() {
        if (mWifiManager == null) {
            mWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        }
        if (mWifiManager != null) {
            if (!mWifiManager.isWifiEnabled()) {
                LogUtils.d("enable wifi");
                mWifiManager.setWifiEnabled(true);
            }
        }
    }

    @Override
    public void disableWifi() {
        if (mWifiManager != null && mWifiManager.isWifiEnabled()) {
            LogUtils.d("disable wifi");
            mWifiManager.setWifiEnabled(false);
        }
    }
}
